# -*- coding: utf-8 -*-
import codecs
import sqlite3
import traceback
import datetime
conn = sqlite3.connect('data.db')
c = conn.cursor()
def deltaToday(id1):
    # 2021-11-30 14:47:05.000000
    d=datetime.datetime.strptime(id1,'%Y-%m-%d %H:%M:%S.%f')
    # (d,t)=id1.split(" ")
    # (y,M,d)=d.split("-")
    # (h,m,s)=t.split(":")
    # d=datetime.datetime(int(y),int(M),int(d),int(h),int(m),int(float(s)))
    d2=datetime.datetime.now()
    # d2=d+datetime.timedelta(1/24/60)
    # print(d)
    # print(d2)
    return(d2-d)
    # return "%04d-%02d-%02d %02d:%02d" % (d2.year,d2.month,d2.day,d2.hour,d2.minute)
def myZhengli(raw):
    print(dir(raw))
    fs=raw.description
    res=[]
    for one in raw:
        o1={}
        i=0
        for a in one:
            o1[fs[i][0]]=a
            i+=1
        res.append(o1)
    return res
def testRaw():
    cmd="select * from parts_contact  where work_month IS NULL   and tiaoshi_date between '2016-10-17' and '2016-12-17'"
    raw=c.execute(cmd)
    r=myZhengli(raw)    
    print(r)
def update():
    # c.execute('alter table parts_contact add column dianqi varchar(30)')    
    # c.execute('alter table parts_contact add column jixie varchar(30)')    
    # c.execute('alter table parts_contact add column hongwai varchar(30)')    
    # c.execute('alter table parts_contact add column redao varchar(30)')    
    c.execute('alter table parts_contact add column work_month date')    
    conn.commit()
def addSampleName():#update result set sampleid=datetime(sampleid,"+0.5 days")
    c.execute('select * from result')
    last=None
    for row in c:
        last=row
    print(last)
    delta=deltaToday(last[0])
    print(delta)
    c.execute('select * from result')
    for row in c:
        last=row
        print(last[0])
    return 
    # We can also close the cursor if we are done with it
    c.close()
    def remove(cmd):
        cs=cmd.split("\n")
        r=""
        for c in cs:
            if c[:2]=="--":
                pass
            else:
                r+=c+"\n"
        return r
def updateDb():
    cmds=codecs.open("tableStruct.sql","r",'utf-8').read().split(";")
    for cmd in cmds:
        try:
            print(cmd)
            c.execute(cmd)
        except sqlite3.OperationalError as e:
            traceback.print_exc()
            if "already exists" in str(e):
                pass
            else:
                pass
                # input("pause")
        except sqlite3.IntegrityError as e:
            pass
    conn.commit()
addSampleName()    
